package part15;

/**
 * @author wcj
 * @Date 2021/5/28 20:30
 * @Version 1.0
 */
public class Part154<T> {

    /**
     * 泛型实现栈
     *
     * @param <U>
     */
    private static class Node<U> {
        U item;
        Node<U> next;

        Node() {
            item = null;
            next = null;
        }

        Node(U item, Node<U> next) {
            this.item = item;
            this.next = next;
        }

        public boolean end() {
            return item == null && next == null;
        }
    }

    /**
     * 哨兵节点，用来判断栈是否为空
     */
    private Node<T> top = new Node<>();

    public void push(T item) {
        top = new Node<>(item, top);
    }

    public T pop() {
        T result = top.item;
        if (!top.end()) {
            top = top.next;
        }
        return result;
    }

    public static void main(String[] args) {
        Part154<String> stringPart154 = new Part154<>();
        for (String s : "wcj love bss".split(" ")) {
            stringPart154.push(s);
        }
        String s;
        while ((s = stringPart154.pop()) != null) {
            System.out.println(s);
        }
    }
}
